<?php
/**
 * 问题集
 * @author weihu
 *
 */
class Faq extends MY_Admin_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Faq_model');
    }
    
    //问题列表
    public function index()
    {
        $question = $this->input->get_post('question');
        $answer = $this->input->get_post('answer');
        $type = $this->input->get_post('type');
        
        $page     = _get_page();
        $pagesize = 10;
        $arrParam = array();
        $arrWhere = array();
        
        if($question)
        {
            $arrParam['question'] = $question;
            $arrWhere['question like '] = "'%$question%'";
        }
        
        if ($answer)
        {
            $arrParam['answer'] = $answer;
            $arrWhere['answer like '] = "'%$answer%'";
        }
        if ($type)
        {
            $arrParam['type'] = $type;
            $arrWhere['type'] = $type;
        }
        
        $strOrder = 'id desc';
        
        $list = $this->Faq_model->fetch_page($page, $pagesize, $arrWhere, 'id,question,answer,type,update_time,op_username', $strOrder);
        //echo $this->db->last_query();die;
        
        //分页
        $pagecfg = array();
        $pagecfg['base_url']     = _create_url('admin/faq', $arrParam);
        $pagecfg['total_rows']   = $list['count'];
        $pagecfg['cur_page'] = $page;
        $pagecfg['per_page'] = $pagesize;
        //$this->load->library('pagination');
        $this->pagination->initialize($pagecfg);
        $list['pages'] = $this->pagination->create_links();
        
        $result = array(
            'list' => $list,
            'arrParam' => $arrParam,
        );
        
        $this->load->view('admin/faq_list',$result);
    }
    
    //添加资讯
    public function add()
    {
        //需要修改
        $id	= _get_key_val($this->input->get('id'), TRUE);
        $result = array();
        $info = array();
        
        if(!empty($id))
        {
            $info = $this->Faq_model->get_by_id($id);
        }
        
        $result = array(
            'info'=>$info,
        );
        
        $this->load->view('admin/faq_add', $result);
    }
    
    //删除资讯
    public function del()
    {
        $id	= _get_key_val($this->input->get('id'), TRUE);
        $page = _get_page();
        
        $this->Faq_model->delete_by_id($id);
        redirect( base_url('/admin/faq?page='.$page) );
    }
    
    //保存
    public function save()
    {
        if ($this->input->is_post())
        {
            //验证规则
            $config = array(
                array(
                    'field'   => 'type',
                    'label'   => '类型',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'question',
                    'label'   => '问题',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'answer',
                    'label'   => '答案',
                    'rules'   => 'trim|required'
                ),
            );
        
            $this->form_validation->set_rules($config);
        
            if ($this->form_validation->run() === TRUE)
            {
                //将需要保存的数据赋值给数组$data
                $data = array(
                    'question'=>$this->input->post('question'),
                    'answer'=>$this->input->post('answer'),
                    'type'=>$this->input->post('type'),
                    'add_time'=>time(),
                    'update_time'=>time(),
                    'op_userid'=>$this->session->userdata['admin_id'],
                    'op_username'=>$this->session->userdata['user_name'],
                );
        
                $id	= _get_key_val($this->input->get('id'), TRUE);
                if($id)
                    $data['id'] = $id;
                //保存至数据库
                $this->Faq_model->insert($data);
        
                //echo '成功,<a href="/admin/aa">返回列表页</a>';
                redirect(base_url('/admin/faq'));
                exit;
            }
            else
            {
                $id	= _get_key_val($this->input->get('id'), TRUE);
                $result = array();
        
                if(!empty($id))
                {
                    $info = $this->Faq_model->get_by_id($id);
                    $result = array(
                        'info'=>$info,
                    );
                }
                $this->load->view('admin/faq_add',$result);
            }
        }
    }
}